package data.dao;

import data.entity.UserEntity;
import data.entity.User_Entity;
import data.vo.Privilege;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by Administrator on 2016/11/3.
 */
public class MyJdbcQuery {

    public MyJdbcQuery(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    private JdbcTemplate jdbcTemplate;

    public UserEntity getUser(Integer id) {
        UserEntity user = jdbcTemplate.queryForObject(
                "select * from t_user where id = ?", new BeanPropertyRowMapper<UserEntity>(UserEntity.class), id);
        System.out.println(user.toString());
        return user;
    }

    public UserEntity getUserByName(String userName) {
        UserEntity user = jdbcTemplate.queryForObject(
                "select * from t_user where name = ?", new BeanPropertyRowMapper<UserEntity>(UserEntity.class), userName);
        System.out.println(user.toString());
        return user;

    }

    public List<Privilege> getUserPrivilege(int userId) {
        String sql = "select privilege from user_privilege where user_id = ?";
        List<String> strings = jdbcTemplate.queryForList(sql, String.class, userId);
        List<Privilege> privileges = new ArrayList<Privilege>(strings.size());
        privileges.addAll(strings.stream().map(Privilege::valueOf).collect(Collectors.toList()));
        return privileges;
    }

    public User_Entity getUser4Auth(String userName) {
        User_Entity user = jdbcTemplate.queryForObject(
                "select * from user_ where username = ?", new BeanPropertyRowMapper<User_Entity>(User_Entity.class), userName);
        return user;
    }

}
